<?php
namespace Home\Controller;

use Think\Controller;
use Common\Service\Curl;
use Think\Log;

class VideoController extends Controller
{

	/**
     * 首页
     */
	public function index(){
		$userId = I('userId', '', 'trim');
		$type = I('type', '', 'trim');

		$page = I('page', '1', 'trim');
        $size = I('size', '5', 'trim'); 
        $firstNum = ($page-1)*$size;
        
        if(!$userId){
        	$this->ajaxReturn(['status'=>false, 'code'=>1001, 'msg'=>'请登录']);
        }

        if($type){
        	$teacher_id = M('teacher')->where(['type'=>$type])->field('id')->getField('id', true);
        	if($teacher_id){ 
        		$map['teacher_id'] = ['in',$teacher_id];
        	}else{
        		$map['teacher_id'] = '';
        	}        	

        }


        $tmp = M('video')->where($map)->order('id desc')->limit($firstNum,$size)->select();

        foreach ($tmp as $k => $v) {

            if(M('user_collect')->where(["user_id"=>$userId,"video_id"=>$v['id']])->find()){
                $tmp[$k]['isCollect'] = 1;
            }else{
                $tmp[$k]['isCollect'] = 0;
            }

            $tmp[$k]['collectCount'] = M("user_collect")->where(['video_id'=>$v['id']])->count();
            $tmp[$k]['commentCount'] = M("comment")->where(['video_id'=>$v['id']])->count();
        }


        if($page == 1 && !$tmp){
            $this->ajaxReturn(['status'=>true, 'code'=>1000, 'msg'=>'暂无视频信息', 'data'=>[]]);
        }
    
        if($page > 1 && !$tmp){
            $this->ajaxReturn(['status'=>false, 'code'=>1002, 'msg'=>'无更多视频消息', 'data'=>[]]);
        }

        foreach ($tmp as $k => $v) {
            $tmp[$k]['video_url'] = base64_encode($v['video_url']);
        }

        $this->ajaxReturn(['status' => true, 'code' => 1000,'msg' => '查询成功', 'data' => $tmp]);

	}

    //期数
    public function period(){
        $userId = I('userId', '', 'trim');

        $page = I('page', '1', 'trim');
        $size = I('size', '5', 'trim'); 
        $firstNum = ($page-1)*$size;
        
        if(!$userId){
            $this->ajaxReturn(['status'=>false, 'code'=>1001, 'msg'=>'请登录']);
        }

        $tmp = M('period')->order('id desc')->limit($firstNum,$size)->select(); 

        if($page == 1 && !$tmp){
            $this->ajaxReturn(['status'=>true, 'code'=>1000, 'msg'=>'暂无视频信息', 'data'=>[]]);
        }
    
        if($page > 1 && !$tmp){
            $this->ajaxReturn(['status'=>false, 'code'=>1002, 'msg'=>'无更多视频消息', 'data'=>[]]);
        }

        $this->ajaxReturn(['status' => true, 'code' => 1000,'msg' => '查询成功', 'data' => $tmp]);
     
    }

    //期数教师介绍
    public function info(){
        $userId = I('userId', '', 'trim');
        $period_id = I('periodId', '', 'trim');;

        $page = I('page', '1', 'trim');
        $size = I('size', '5', 'trim'); 
        $firstNum = ($page-1)*$size;
        
        if(!$userId){
            $this->ajaxReturn(['status'=>false, 'code'=>1001, 'msg'=>'请登录']);
        }

        $info = M('period')->where(['id'=>$period_id])->find(); 
    
        $teacher_arr = explode(',', $info['teacher_info']);

        $count = count($teacher_arr);//总条数
        $start=($page-1)*$size;//偏移量，当前页-1乘以每页显示条数
        $teacher_arr = array_slice($teacher_arr,$start,$size);


        if($page == 1 && !$teacher_arr){
            $this->ajaxReturn(['status'=>true, 'code'=>1000, 'msg'=>'暂无视频信息', 'data'=>[]]);
        }
    
        if($page > 1 && !$teacher_arr){
            $this->ajaxReturn(['status'=>false, 'code'=>1002, 'msg'=>'无更多视频消息', 'data'=>[]]);
        }

        $where['id'] = ['in',$teacher_arr];
        $tmp = M("teacher")->where($where)->select();

        $this->ajaxReturn(['status' => true, 'code' => 1000,'msg' => '查询成功', 'data' => $tmp]);
     
    }

	//搜索
	public function search(){

		$userId = I('userId', '', 'trim');
		$key = I('key', '', 'trim');
		$page = I('page', '1', 'trim');
        $size = I('size', '5', 'trim'); 
        $firstNum = ($page-1)*$size;

        if(!$userId){
        	$this->ajaxReturn(['status'=>false, 'code'=>1001, 'msg'=>'请登录']);
        }

        if(!$key){
        	$this->ajaxReturn(['status'=>false, 'code'=>1002, 'msg'=>'请输入搜索关键字']);
        }

		if($key){
			$map['title'] = ["like","%$key%"];
            $map['teacher_name'] = ["like","%$key%"];
            $map['_logic'] = 'or';
		}

		$model = M('video as s');
		$res = $model
			->where($map)
			//->field('s.seller_id,s.seller_name,s.logo_url,s.phone,s.address,s.id')
			->limit($firstNum,$size)
			->select();
	
		if($page == 1 && !$res){
            $this->ajaxReturn(['status'=>true, 'code'=>1000, 'msg'=>'该查询条件下没有视频信息', 'data'=>[]]);
        }
    
        if($page > 1 && !$res){
            $this->ajaxReturn(['status'=>false, 'code'=>1002, 'msg'=>'无更多视频数据', 'data'=>[]]);
        }

        foreach ($res as $k => $v) {
            $res[$k]['video_url'] = base64_encode($v['video_url']);
        }


		$this->ajaxReturn(['status' => true, 'code'=>1000,'msg' => '查询成功','data'=>$res]);
	}

	/**
     * 作品详情
     */
	public function detail(){
		$userId = I('userId', '', 'trim');
		$videoId = I('videoId', '', 'trim');

		if(!$userId){
        	$this->ajaxReturn(['status'=>false, 'code'=>1001, 'msg'=>'请登录']);
        }

        if(!$videoId){
        	$this->ajaxReturn(['status'=>false, 'code'=>1002, 'msg'=>'参数异常']);
        }

        $res = M('video as v')
            ->field("v.*,t.nickname,t.headimg")
            ->join('left join wk_teacher as t on t.id=v.teacher_id')
            ->where(['v.id'=>$videoId])->find();

        $page = I('page', '1', 'trim');
        $size = I('size', '5', 'trim'); 
        $firstNum = ($page-1)*$size;

        if(M('user_collect')->where(["user_id"=>$userId,"video_id"=>$videoId])->find()){
            $res['isCollect'] = 1;
        }else{
            $res['isCollect'] = 0;
        }

        $res['video_url'] = base64_encode($res['video_url']);

        $res['collectCount'] = M("user_collect")->where(['video_id'=>$videoId])->count();
        $res['commentCount'] = M("comment")->where(['video_id'=>$videoId])->count();

        $comment = M('comment c')
                ->field("c.*,u.nickname,u.headimg")
                ->join('left join wk_user as u on u.id=c.user_id')
                ->where(['video_id'=>$videoId])->limit($firstNum,$size)->select();


        /*if($page == 1 && !$comment){
            $this->ajaxReturn(['status' => true, 'code'=>1000,'msg' => '查询成功','data'=>['videoInfo'=>$res,'comment'=>[]]]);
        }
    
        if($page > 1 && !$comment){
            $this->ajaxReturn(['status' => true, 'code'=>1000,'msg' => '查询成功','data'=>['videoInfo'=>$res,'comment'=>[]]]);
        }*/


        $this->ajaxReturn(['status' => true, 'code'=>1000,'msg' => '查询成功','data'=>['videoInfo'=>$res,'commentInfo'=>$comment]]);

	}

    /**
     * 视频评论
     */
    public function addComment(){
        $userId = I('userId', '', 'trim');
        $videoId = I('videoId', '', 'trim');
        $content = I('content', '', 'trim');

        if(!$userId){
            $this->ajaxReturn(['status'=>false, 'code'=>1001, 'msg'=>'请登录']);
        }

        if(!$videoId){
            $this->ajaxReturn(['status'=>false, 'code'=>1002, 'msg'=>'参数异常']);
        }

        if(!$content){
            $this->ajaxReturn(['status'=>false, 'code'=>1003, 'msg'=>'请输入评论内容']);
        }

        $data = [
                    'user_id'  => $userId,
                    'video_id' => $videoId,
                    'content'  => $content,
                    'addtime'  => time(),
                    'updatetime' => time()

                ];

        $id = M('comment')->add($data);
        if($id){
            $this->ajaxReturn(['status' => true, 'code'=>1000,'msg' => '评论成功','data'=>['id'=>$id]]);
        } else{
            $this->ajaxReturn(['status' => false, 'code'=>1004,'msg' => '网络异常','data'=>[]]);
        }  

       
        
    }
}
